<template>
  <n-layout has-sider class="main-layout">
    <n-layout-sider
      bordered
      show-trigger
      collapse-mode="width"
      :collapsed-width="64"
      :width="240"
      :native-scrollbar="false"
    >
      <div class="logo">
        <n-icon :size="32" color="#18A058">
          <BookOutline />
        </n-icon>
        <span class="logo-text">图书管理</span>
      </div>
      
      <n-menu
        :collapsed-width="64"
        :collapsed-icon-size="22"
        :options="menuOptions"
        :value="currentRoute"
        @update:value="handleMenuClick"
      />
    </n-layout-sider>
    
    <n-layout>
      <n-layout-header bordered class="header">
        <div class="header-content">
          <h2>{{ currentTitle }}</h2>
          <div class="user-info">
            <n-dropdown :options="userOptions" @select="handleUserAction">
              <n-button text>
                <template #icon>
                  <n-icon :size="20">
                    <PersonCircleOutline />
                  </n-icon>
                </template>
                {{ userStore.user?.nickname || userStore.user?.username }}
              </n-button>
            </n-dropdown>
          </div>
        </div>
      </n-layout-header>
      
      <n-layout-content content-style="padding: 24px;" :native-scrollbar="false">
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </n-layout-content>
    </n-layout>
  </n-layout>
</template>

<script setup>
import { ref, computed, h } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { NLayout, NLayoutSider, NLayoutHeader, NLayoutContent, NMenu, NButton, NDropdown, NIcon, useMessage } from 'naive-ui'
import { 
  GridOutline, 
  BookOutline, 
  ListOutline, 
  TimeOutline, 
  PeopleOutline, 
  PersonCircleOutline,
  LogOutOutline,
  PersonOutline
} from '@vicons/ionicons5'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const message = useMessage()

const currentRoute = computed(() => route.name)
const currentTitle = computed(() => route.meta.title || '')

const iconMap = {
  GridOutline,
  BookOutline,
  ListOutline,
  TimeOutline,
  PeopleOutline,
  PersonOutline
}

const menuOptions = computed(() => {
  const routes = router.options.routes.find(r => r.path === '/')?.children || []
  return routes
    .filter(route => !route.meta?.hidden && (!route.meta?.role || route.meta.role === userStore.user?.role))
    .map(route => ({
      label: route.meta?.title,
      key: route.name,
      icon: () => h(NIcon, { size: 20 }, { default: () => h(iconMap[route.meta?.icon]) })
    }))
})

const userOptions = [
  {
    label: '个人中心',
    key: 'profile',
    icon: () => h(NIcon, { size: 18 }, { default: () => h(PersonOutline) })
  },
  {
    label: '退出登录',
    key: 'logout',
    icon: () => h(NIcon, { size: 18 }, { default: () => h(LogOutOutline) })
  }
]

const handleMenuClick = (key) => {
  router.push({ name: key })
}

const handleUserAction = async (key) => {
  if (key === 'logout') {
    await userStore.logout()
    message.success('已退出登录')
    router.push('/login')
  } else if (key === 'profile') {
    router.push('/profile')
  }
}
</script>

<style scoped>
.main-layout {
  height: 100vh;
}

.logo {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  font-size: 20px;
  font-weight: bold;
  color: #18A058;
  border-bottom: 1px solid #e0e0e0;
}


.header {
  height: 64px;
  display: flex;
  align-items: center;
  padding: 0 24px;
}

.header-content {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 16px;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.2s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>

